﻿namespace ACCOUNTING.LVY.Business
{
    using ACCOUNTING;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class clsChungTuTongHop
    {
        private string mChungTuDinhKem = "";
        private string mDiaChi = "";
        private string mDienGiai = "";
        private string mDienThoai = "";
        private string mKhoa = "";
        private string mKhoaBoPhan = "";
        private string mKhoaDoiTuong = "";
        private string mKhoaDonVi = "";
        private string mKhoaHopDong = "";
        private string mKhoaKhoanMuc = "";
        private string mKhoaNhanVienCapNhat = "";
        private string mKhoaSanPham = "";
        private string mKhoaTKChu = "";
        private string mLoaiChungTu = "";
        private string mLoaiTien = "";
        private string mNamThang = "";
        private string mNgayCapNhat = "";
        private string mNgayChungTu = "";
        private string mNguoiDaiDien = "";
        private int mSend = 0;
        private string mSoChungTu = "";
        private string mSoCMND = "";
        private double mSoTien = 0.0;
        private double mSoTienNT = 0.0;
        private double mTyGia = 0.0;

        public void ClearTemp(string pKeyTable)
        {
            string commandText = "DELETE FROM GL_TongHopChiTietTmp WHERE KhoaChungTu = '" + pKeyTable.Trim() + "'";
            DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]);
        }

        public bool DeleteData(string strKhoa)
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[4];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@Khoa", DbType.Int64, ParameterDirection.Input, strKhoa);
                commandParameters[1] = DBHelper.createParameter("@CheckAmQuy", DbType.Double, ParameterDirection.Input, 1);
                commandParameters[2] = DBHelper.createParameter("@AmQuy", DbType.Double, ParameterDirection.Output, 0);
                commandParameters[3] = DBHelper.createParameter("@pError", DbType.Double, ParameterDirection.Output, 0);
                int num = DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "GL_sp_ChungTuTongHopDel", commandParameters);
                if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectNotEqual(commandParameters[2].Value, 0, false))
                {
                    if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectNotEqual(commandParameters[3].Value, 0, false))
                    {
                        LVYModule.ShowWarning(modGeneral.Tranlated("Nghiệp vụ kh\x00f4ng thể thực hiện được v\x00ec \x00e2m quỹ!", ""));
                    }
                    return false;
                }
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public DataTable GetDataPrint(string strKhoa)
        {
            DataTable table;
            try
            {
                string commandText = "SELECT CTTH.SoChungTu, CTTH.NgayChungTu as NgayChungTu,DTN.TenVIET  as DoiTuongNo,DTN.TenVIET  as DoiTuongCo,TK.SoTaiKhoan as TKNo,  CT.SoTienNT as SoTien, CT.SoTien as QuyDoi,CT.DienGiai,TKK.SoTaiKhoan as TKCo, CTTH.ChungTuDinhKem, CTTH.DienGiai as NoiDung FROM GL_TongHop CTTH LEFT JOIN GL_TongHopChiTiet CT on CTTH.Khoa = CT.KhoaChungTu LEFT JOIN DM_DoiTuong DTN on CT.KhoaDoiTuongNo = DTN.Khoa LEFT JOIN DM_DoiTuong DTC on CT.KhoaDoiTuongCo = DTC.Khoa LEFT JOIN DM_TaiKhoan TK on CT.KhoaTaiKhoanNo = TK.Khoa LEFT JOIN DM_TaiKhoan TKK on CT.KhoaTaiKhoanCo = TKK.Khoa Where CTTH.Khoa = '" + strKhoa + "'";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable GetList(string strCondition = "")
        {
            DataTable table;
            string str2 = "";
            try
            {
                if (strCondition.Trim() != "")
                {
                    str2 = " WHERE " + strCondition;
                }
                string commandText = "SELECT Khoa, SoChungTu, dbo.Char2Date(NgayChungTu) as Ngay, ChungTuDinhKem,  SoTienNT, SoTien, DienGiai FROM GL_TongHop " + str2 + " ORDER BY NgayChungTu, SoChungTu ";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable GetListDetails(string strKhoa)
        {
            DataTable table;
            try
            {
                string commandText = " SELECT CT.Khoa, CT.KhoaTaiKhoan, TK.SoTaiKhoan, CT.KhoaDoiTuong, DT.Ten" + modGeneral.H_LANGUAGE + " as DoiTuong, CT.KhoaChungTuThamChieu, CT.SoChungTuThamChieu,  CT.SoTienNT, CT.SoTien, CT.KhoaTaiKhoanThue, TKT.SoTaiKhoan as TaiKhoanThue, CT.KhoaBoPhan, BP.Ten" + modGeneral.H_LANGUAGE + " as BoPhan,  CT.KhoaSanPham, SP.Ten" + modGeneral.H_LANGUAGE + " as SanPham, CT.KhoaHopDong, HD.SoHopDong, CT.Khoa, CT.KhoaLoaiThue, LT.Ma as LoaiThue,  CT.TyLeThue, CT.TienThueNT, CT.TienThue, CT.SoHoaDon, dbo.Char2Date(CT.NgayHoaDon) as NgayHoaDon,  CT.SoSeri, CT.KhoaDoiTuongThue, CT.TenDoiTuongThue, CT.MaSoThue, CT.DiaChiThue, CT.DienGiai,  CT.KhoaLoaiChungTu, CP.Ten" + modGeneral.H_LANGUAGE + " as ChiPhi, CT.Ve  FROM GL_TongHopChiTiet CT LEFT JOIN DM_TaiKhoan TK on CT.KhoaTaiKhoan = TK.Khoa  LEFT JOIN DM_TaiKhoan TKT on CT.KhoaTaiKhoanThue = TKT.Khoa  LEFT JOIN DM_DoiTuong DT on CT.KhoaDoiTuong = DT.Khoa  LEFT JOIN DM_DonViBoPhan BP on CT.KhoaBoPhan = BP.Khoa  LEFT JOIN DM_SanPhamCongViec SP on CT.KhoaSanPham = SP.Khoa  LEFT JOIN DM_HopDong HD on CT.KhoaHopDong = HD.Khoa  LEFT JOIN DM_LoaiHoaDon LT on CT.KhoaLoaiThue = LT.Khoa  LEFT JOIN DM_KhoanMucChiPhi CP on CT.KhoaLoaiChungTu = CP.Khoa  WHERE CT.KhoaChungTu = '" + strKhoa + "'";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable GetNhatKyChungTu(string strTuNgay, string strDenNgay, string strKhoaDonVi, string strConditions = "")
        {
            DataTable table;
            string str2 = "";
            if (strConditions.Trim() != "")
            {
                str2 = " AND " + strConditions;
            }
            try
            {
                string commandText = "  SELECT NK.KhoaChungTu, NK.SoChungTu, dbo.Char2Date(NK.NgayChungTu) as Ngay,  NK.SoHoaDon, TKN.SoTaiKhoan as TaiKhoanNo, TKC.SoTaiKhoan As TaiKhoanCo, NK.LoaiTien, NK.SoPhatSinh, NK.DienGiai  FROM GL_NhatKyChungTu NK  LEFT JOIN DM_TaiKhoan TKN on NK.KhoaTaiKhoanNo = TKN.Khoa  LEFT JOIN DM_TaiKhoan TKC on NK.KhoaTaiKhoanCo = TKC.Khoa  WHERE NK.KhoaDonVi = '" + strKhoaDonVi.Trim() + "' and (NK.NgayChungTu Between '" + strTuNgay + "' and '" + strDenNgay + "') " + str2;
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public bool IsDuplicateVoucherNo(string strVoucherNo, string strKeyTable)
        {
            string commandText = "SELECT * FROM GL_TongHop Where Rtrim(SoChungTu) = '" + strVoucherNo.Trim() + "' AND Khoa <> '" + strKeyTable + "'";
            return (DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0].Rows.Count > 0);
        }

        public bool Load(string pKhoa)
        {
            bool flag=false;
            try
            {
                string commandText = "SELECT * FROM GL_TongHop WHERE Khoa = '" + pKhoa + "'";
                IDataReader dataReader = DBHelper.ExecuteReader(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]);
                if (dataReader.Read())
                {
                    this.mKhoa = Conversions.ToString(modGeneral.GetValue(dataReader, 0, modGeneral.typeOfColumn.GetString));
                    this.mNamThang = Conversions.ToString(modGeneral.GetValue(dataReader, 1, modGeneral.typeOfColumn.GetString));
                    this.mSoChungTu = Conversions.ToString(modGeneral.GetValue(dataReader, 2, modGeneral.typeOfColumn.GetString));
                    this.mNgayChungTu = Conversions.ToString(modGeneral.GetValue(dataReader, 3, modGeneral.typeOfColumn.GetString));
                    this.mKhoaDoiTuong = Conversions.ToString(modGeneral.GetValue(dataReader, 4, modGeneral.typeOfColumn.GetString));
                    this.mKhoaBoPhan = Conversions.ToString(modGeneral.GetValue(dataReader, 5, modGeneral.typeOfColumn.GetString));
                    this.mKhoaSanPham = Conversions.ToString(modGeneral.GetValue(dataReader, 6, modGeneral.typeOfColumn.GetString));
                    this.mKhoaHopDong = Conversions.ToString(modGeneral.GetValue(dataReader, 7, modGeneral.typeOfColumn.GetString));
                    this.mDiaChi = Conversions.ToString(modGeneral.GetValue(dataReader, 8, modGeneral.typeOfColumn.GetString));
                    this.mDienThoai = Conversions.ToString(modGeneral.GetValue(dataReader, 9, modGeneral.typeOfColumn.GetString));
                    this.mNguoiDaiDien = Conversions.ToString(modGeneral.GetValue(dataReader, 10, modGeneral.typeOfColumn.GetString));
                    this.mSoCMND = Conversions.ToString(modGeneral.GetValue(dataReader, 11, modGeneral.typeOfColumn.GetString));
                    this.mDienGiai = Conversions.ToString(modGeneral.GetValue(dataReader, 12, modGeneral.typeOfColumn.GetString));
                    this.mLoaiTien = Conversions.ToString(modGeneral.GetValue(dataReader, 13, modGeneral.typeOfColumn.GetString));
                    this.mTyGia = Conversions.ToDouble(modGeneral.GetValue(dataReader, 14, modGeneral.typeOfColumn.GetDecimal));
                    this.mSoTienNT = Conversions.ToDouble(modGeneral.GetValue(dataReader, 15, modGeneral.typeOfColumn.GetDecimal));
                    this.mSoTien = Conversions.ToDouble(modGeneral.GetValue(dataReader, 0x10, modGeneral.typeOfColumn.GetDecimal));
                    this.mLoaiChungTu = Conversions.ToString(modGeneral.GetValue(dataReader, 0x11, modGeneral.typeOfColumn.GetString));
                    this.mKhoaTKChu = Conversions.ToString(modGeneral.GetValue(dataReader, 0x12, modGeneral.typeOfColumn.GetString));
                    this.mChungTuDinhKem = Conversions.ToString(modGeneral.GetValue(dataReader, 0x13, modGeneral.typeOfColumn.GetString));
                    this.mKhoaDonVi = Conversions.ToString(modGeneral.GetValue(dataReader, 20, modGeneral.typeOfColumn.GetString));
                    this.mKhoaNhanVienCapNhat = Conversions.ToString(modGeneral.GetValue(dataReader, 0x15, modGeneral.typeOfColumn.GetString));
                    this.mNgayCapNhat = Conversions.ToString(modGeneral.GetValue(dataReader, 0x16, modGeneral.typeOfColumn.GetString));
                    this.mSend = Conversions.ToInteger(modGeneral.GetValue(dataReader, 0x17, modGeneral.typeOfColumn.GetInt32));
                    this.mKhoaKhoanMuc = Conversions.ToString(modGeneral.GetValue(dataReader, 0x18, modGeneral.typeOfColumn.GetString));
                }
                dataReader.Close();
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public bool Save()
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[0x1b];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@Khoa", DbType.Int64, ParameterDirection.Input, this.mKhoa);
                commandParameters[1] = DBHelper.createParameter("@NamThang", DbType.Int64, ParameterDirection.Input, this.mNamThang);
                commandParameters[2] = DBHelper.createParameter("@SoChungTu", DbType.Int64, ParameterDirection.Input, this.mSoChungTu);
                commandParameters[3] = DBHelper.createParameter("@NgayChungTu", DbType.Int64, ParameterDirection.Input, this.mNgayChungTu);
                commandParameters[4] = DBHelper.createParameter("@KhoaDoiTuong", DbType.Int64, ParameterDirection.Input, this.mKhoaDoiTuong);
                commandParameters[5] = DBHelper.createParameter("@KhoaBoPhan", DbType.Int64, ParameterDirection.Input, this.mKhoaBoPhan);
                commandParameters[6] = DBHelper.createParameter("@KhoaSanPham", DbType.Int64, ParameterDirection.Input, this.mKhoaSanPham);
                commandParameters[7] = DBHelper.createParameter("@KhoaHopDong", DbType.Int64, ParameterDirection.Input, this.mKhoaHopDong);
                commandParameters[8] = DBHelper.createParameter("@DiaChi", DbType.Int64, ParameterDirection.Input, this.mDiaChi);
                commandParameters[9] = DBHelper.createParameter("@DienThoai", DbType.Int64, ParameterDirection.Input, this.mDienThoai);
                commandParameters[10] = DBHelper.createParameter("@NguoiDaiDien", DbType.Int64, ParameterDirection.Input, this.mNguoiDaiDien);
                commandParameters[11] = DBHelper.createParameter("@SoCMND", DbType.Int64, ParameterDirection.Input, this.mSoCMND);
                commandParameters[12] = DBHelper.createParameter("@DienGiai", DbType.Int64, ParameterDirection.Input, this.mDienGiai);
                commandParameters[13] = DBHelper.createParameter("@LoaiTien", DbType.Int64, ParameterDirection.Input, this.mLoaiTien);
                commandParameters[14] = DBHelper.createParameter("@TyGia", DbType.Date, ParameterDirection.Input, this.mTyGia);
                commandParameters[15] = DBHelper.createParameter("@SoTienNT", DbType.Date, ParameterDirection.Input, this.mSoTienNT);
                commandParameters[0x10] = DBHelper.createParameter("@SoTien", DbType.Date, ParameterDirection.Input, this.mSoTien);
                commandParameters[0x11] = DBHelper.createParameter("@LoaiChungTu", DbType.Int64, ParameterDirection.Input, this.mLoaiChungTu);
                commandParameters[0x12] = DBHelper.createParameter("@KhoaTKChu", DbType.Int64, ParameterDirection.Input, this.mKhoaTKChu);
                commandParameters[0x13] = DBHelper.createParameter("@ChungTuDinhKem", DbType.Int64, ParameterDirection.Input, this.mChungTuDinhKem);
                commandParameters[20] = DBHelper.createParameter("@KhoaDonVi", DbType.Int64, ParameterDirection.Input, this.mKhoaDonVi);
                commandParameters[0x15] = DBHelper.createParameter("@KhoaNhanVienCapNhat", DbType.Int64, ParameterDirection.Input, this.mKhoaNhanVienCapNhat);
                commandParameters[0x16] = DBHelper.createParameter("@NgayCapNhat", DbType.Int64, ParameterDirection.Input, this.mNgayCapNhat);
                commandParameters[0x17] = DBHelper.createParameter("@Send", DbType.Double, ParameterDirection.Input, this.mSend);
                commandParameters[0x18] = DBHelper.createParameter("@KhoaKhoanMuc", DbType.Int64, ParameterDirection.Input, this.mKhoaKhoanMuc);
                commandParameters[0x19] = DBHelper.createParameter("@AmQuy", DbType.Double, ParameterDirection.Output, 0);
                commandParameters[0x1a] = DBHelper.createParameter("@pError", DbType.Double, ParameterDirection.Output, 0);
                int num = DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "GL_sp_ChungTuTongHop", commandParameters);
                if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectEqual(commandParameters[0x1a].Value, 0, false))
                {
                    return true;
                }
                if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectNotEqual(commandParameters[0x19].Value, 0, false))
                {
                    LVYModule.ShowWarning(modGeneral.Tranlated("Nghiệp vụ kh\x00f4ng thể thực hiện được v\x00ec \x00e2m quỹ!", ""));
                }
                flag = false;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public string ChungTuDinhKem
        {
            get
            {
                return this.mChungTuDinhKem;
            }
            set
            {
                this.mChungTuDinhKem = value;
            }
        }

        public string DiaChi
        {
            get
            {
                return this.mDiaChi;
            }
            set
            {
                this.mDiaChi = value;
            }
        }

        public string DienGiai
        {
            get
            {
                return this.mDienGiai;
            }
            set
            {
                this.mDienGiai = value;
            }
        }

        public string DienThoai
        {
            get
            {
                return this.mDienThoai;
            }
            set
            {
                this.mDienThoai = value;
            }
        }

        public string Khoa
        {
            get
            {
                return this.mKhoa;
            }
            set
            {
                this.mKhoa = value;
            }
        }

        public string KhoaBoPhan
        {
            get
            {
                return this.mKhoaBoPhan;
            }
            set
            {
                this.mKhoaBoPhan = value;
            }
        }

        public string KhoaDoiTuong
        {
            get
            {
                return this.mKhoaDoiTuong;
            }
            set
            {
                this.mKhoaDoiTuong = value;
            }
        }

        public string KhoaDonVi
        {
            get
            {
                return this.mKhoaDonVi;
            }
            set
            {
                this.mKhoaDonVi = value;
            }
        }

        public string KhoaHopDong
        {
            get
            {
                return this.mKhoaHopDong;
            }
            set
            {
                this.mKhoaHopDong = value;
            }
        }

        public string KhoaKhoanMuc
        {
            get
            {
                return this.mKhoaKhoanMuc;
            }
            set
            {
                this.mKhoaKhoanMuc = value;
            }
        }

        public string KhoaNhanVienCapNhat
        {
            get
            {
                return this.mKhoaNhanVienCapNhat;
            }
            set
            {
                this.mKhoaNhanVienCapNhat = value;
            }
        }

        public string KhoaSanPham
        {
            get
            {
                return this.mKhoaSanPham;
            }
            set
            {
                this.mKhoaSanPham = value;
            }
        }

        public string KhoaTKChu
        {
            get
            {
                return this.mKhoaTKChu;
            }
            set
            {
                this.mKhoaTKChu = value;
            }
        }

        public string LoaiChungTu
        {
            get
            {
                return this.mLoaiChungTu;
            }
            set
            {
                this.mLoaiChungTu = value;
            }
        }

        public string LoaiTien
        {
            get
            {
                return this.mLoaiTien;
            }
            set
            {
                this.mLoaiTien = value;
            }
        }

        public string NamThang
        {
            get
            {
                return this.mNamThang;
            }
            set
            {
                this.mNamThang = value;
            }
        }

        public string NgayCapNhat
        {
            get
            {
                return this.mNgayCapNhat;
            }
            set
            {
                this.mNgayCapNhat = value;
            }
        }

        public string NgayChungTu
        {
            get
            {
                return this.mNgayChungTu;
            }
            set
            {
                this.mNgayChungTu = value;
            }
        }

        public string NguoiDaiDien
        {
            get
            {
                return this.mNguoiDaiDien;
            }
            set
            {
                this.mNguoiDaiDien = value;
            }
        }

        public int Send
        {
            get
            {
                return this.mSend;
            }
            set
            {
                this.mSend = value;
            }
        }

        public string SoChungTu
        {
            get
            {
                return this.mSoChungTu;
            }
            set
            {
                this.mSoChungTu = value;
            }
        }

        public string SoCMND
        {
            get
            {
                return this.mSoCMND;
            }
            set
            {
                this.mSoCMND = value;
            }
        }

        public double SoTien
        {
            get
            {
                return this.mSoTien;
            }
            set
            {
                this.mSoTien = value;
            }
        }

        public double SoTienNT
        {
            get
            {
                return this.mSoTienNT;
            }
            set
            {
                this.mSoTienNT = value;
            }
        }

        public double TyGia
        {
            get
            {
                return this.mTyGia;
            }
            set
            {
                this.mTyGia = value;
            }
        }
    }
}

